package Jets.ui
{
   import flash.display.DisplayObjectContainer;
   import flash.events.Event;
   import flash.events.MouseEvent;
   
   public class JTextArea extends JText
   {
	   protected var _showSlide:Boolean = true;
	   protected var vslide:JVScorllBar;
	   
      public function JTextArea(parent:DisplayObjectContainer=null, cx:Number=0, cy:Number=0, text:String="") 
	  {
         super(parent,cx,cy,text);
      }
      
     
      
      override protected function addChildren() : void 
	  {
         super.addChildren();
         editable = false;
         this.vslide = new JVScorllBar(this,this.width,0,this.scroll);
         this.vslide.visible = false;
         this.vslide.value = 100;
         this.vslide.getDeltaFunc = this.calScrollClickDelta;
         _tf.addEventListener(MouseEvent.MOUSE_WHEEL,this.posvs);
         _tf.addEventListener(Event.SCROLL,this.oninput);
      }
      
      protected function calScrollClickDelta(start:Number, end:Number) : Number 
	  {
         var area:Number = end - start;
         var res:Number = area / this.maxScrollV;
         return res;
      }
      
      public function get maxScrollV() : int 
	  {
         return this._tf.maxScrollV-1;
      }
      
      public function get scrollV() : int 
	  {
         return this._tf.scrollV-1;
      }
      
      public function set scrollV(value:int) : void 
	  {
         this._tf.scrollV = value;
      }
      
      public function resetVS() : void 
	  {
         this.scrollV = this.maxScrollV;
         this.vslide.value = 0;
      }
      
      public function topmostVS() : void 
	  {
         this.scrollV = 0;
         this.vslide.value = this.vslide.maximum;
      }
      
      protected function posvs(param1:MouseEvent) : void 
	  {
         this.vslide.value = this.vslide.value + param1.delta * 2;
         this.invalidate();
      }
      
	  protected function scroll(param1:Event) : void 
	  {
         this.invalidate();
      }
      
      override protected function oninput(param1:Event) : void 
	  {
         super.oninput(param1);
         if(editable)
         {
            this.vslide.value = 100 - this.scrollV * 100 / this.maxScrollV;
            if(_tf.maxScrollV > 1)
            {
               this.vslide.visible = true;
            }
            else
            {
               this.vslide.visible = false;
            }
            invalidate();
         }
      }
      
      
      override public function draw() : void 
	  {
         super.draw();
         this.vslide.height = this.height;
         this.vslide.x = _width - this.vslide.arrowwidth;
         if(this.maxScrollV > 0 && (this._showSlide))
         {
            this.vslide.visible = true;
            var nlen:int = (this.vslide.height - this.vslide.arrowheight * 2) * (_tf.numLines - this.maxScrollV) / _tf.numLines;
            this.vslide.barheight = Math.max(nlen,20);
            var delta:int = Math.round((100 - this.vslide.value) * this.maxScrollV / 100);
            this._tf.scrollV = delta + 1;
            return;
         }
         this.vslide.visible = false;
         this.vslide.barheight = 10;
         this._tf.scrollV = 0;
      }
      
      override public function get textHeight() : Number 
	  {
         _tf.wordWrap = true;
         _tf.multiline = true;
         return _tf.textHeight;
      }
   }
}
